package com.yuyou.fn.activity.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.google.common.collect.Lists;
import com.yuyou.fn.activity.entity.*;
import com.yuyou.fn.activity.service.*;
import com.yuyou.fn.activity.vo.*;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.platform.permission.DataPermission;
import org.springframework.stereotype.Service;

import com.yuyou.fn.activity.mapper.ActivityCampusMapper;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * ActivityCampus 表数据服务层接口实现类
 *
 */
@Service
public class ActivityCampusServiceImpl extends SuperServiceImpl<ActivityCampusMapper, ActivityCampus,Long> implements IActivityCampusService {

    @Resource
    private IActivityTargetService activityTargetService;

    @Resource
    private IActivityRegSituationService activityRegSituationService;

    @Resource
    private IActivityService activityService;

    @Resource
    private IActivityCampusSessionService activityCampusSessionService;

    @Resource
    private ICampusService campusService;

    @Override
    @Transactional
    public void saveActivityCampus(ActivityCampusSaveVo activityCampusSaveVo) {
        Long activityId=activityCampusSaveVo.getActivityId();
        Activity activity=activityService.findById(activityId);
        Date now=TimeUtils.now();
        List<CampusSessionVo> campusSessionList = activityCampusSaveVo.getCampusSessionList();
        Map<Long, CampusSessionVo> sessionVoMap = BeanUtils.toMap(campusSessionList, "campusId");
        List<Long> campusIds = BeanUtils.toList(campusSessionList, "campusId");
        List<ActivityTarget> activityTargetList=activityTargetService.findActivityTargetByActivityIdCampusIds(activityCampusSaveVo.getActivityId(),campusIds);

        List<Long> deleteIds=new ArrayList<>(activityTargetList.size());
        for (ActivityTarget activityTarget:activityTargetList){
            if (!sessionVoMap.containsKey(activityTarget.getCampusId())){
                deleteIds.add(activityTarget.getActivityTargetId());
            }
        }
        if (deleteIds.size()>0){
            activityTargetService.deleteBatchIds(deleteIds);
        }
        Map<Long, ActivityTarget> activityTargetMap = BeanUtils.toMap(activityTargetList, "campusId");
        List<ActivityTarget> saveList=new ArrayList<>(campusIds.size());
        for (Long campusId:campusIds){
            if (!activityTargetMap.containsKey(campusId)){
                ActivityTarget activityTarget=new ActivityTarget();
                activityTarget.setActivityId(activityId);
                activityTarget.setAreaTeamId(activity.getAreaTeamId());
                activityTarget.setActivityName(activity.getActivityName());
                activityTarget.setActivityStartDate(activity.getActivityStartDate());
                activityTarget.setCampusId(campusId);
                activityTarget.setCreateTime(now);
                activityTarget.setUpdateTime(now);
                activityTarget.setSignStatus(0);
                saveList.add(activityTarget);
            }
        }
        if (saveList.size()>0){
            activityTargetService.insertBatch(saveList);
        }



        this.deleteActivityCampusByActivityId(activityId);
        activityCampusSessionService.deleteSessionByActivityId(activityId);
        if (Utils.checkListNotEmpty(campusSessionList)){
            List<ActivityCampus> activityCampusList=new ArrayList<>(campusSessionList.size());
            List<ActivityCampusSession> activityCampusSessionList=new ArrayList<>();
             for (CampusSessionVo campusSessionVo:campusSessionList){
                 if (campusSessionVo.getTotalSession()==null || campusSessionVo.getTotalSession()==0){
                     continue;
                 }
                ActivityCampus activityCampus=new ActivityCampus();
                Long activityCampusId=campusSessionVo.getActivityCampusId()==null? IdWorker.getId():campusSessionVo.getActivityCampusId();
                activityCampus.setActivityCampusId(activityCampusId);
                activityCampus.setCampusId(campusSessionVo.getCampusId());
                activityCampus.setActivityId(activityId);
                activityCampus.setActivityStartDate(activity.getActivityStartDate());
                activityCampus.setActivityName(activity.getActivityName());
                activityCampus.setTotalSession(campusSessionVo.getTotalSession());
                activityCampus.setCreateTime(now);
                activityCampus.setUpdateTime(now);
                activityCampusList.add(activityCampus);

                List<SessionDetailVo> sessionDetailVoList=campusSessionVo.getActivityCampusSessionList();
                if (sessionDetailVoList!=null){
                    for (SessionDetailVo sessionDetailVo:sessionDetailVoList){
                        ActivityCampusSession activityCampusSession=new ActivityCampusSession();
                        activityCampusSession.setActivityCampusSessionId(sessionDetailVo.getActivityCampusSessionId());
                        activityCampusSession.setActivityId(activityId);
                        activityCampusSession.setActivityCampusId(activityCampusId);
                        activityCampusSession.setSessionNo(sessionDetailVo.getSessionNo());
                        activityCampusSession.setSessionMsg(sessionDetailVo.getSessionMsg());
                        activityCampusSession.setShowStatus(sessionDetailVo.getShowStatus());
                        activityCampusSession.setCreateTime(now);
                        activityCampusSession.setUpdateTime(now);
                        activityCampusSessionList.add(activityCampusSession);
                    }
                }

            }
            if (activityCampusList.size()>0){
                this.insertBatch(activityCampusList);
            }
            if (activityCampusSessionList.size()>0){
                activityCampusSessionService.insertBatch(activityCampusSessionList);
            }
        }
    }

    @Override
    public void deleteActivityCampusByActivityId(long activityId) {
        Example<ActivityCampus> example=ActivityCampus.createExample();
        example.createCriteria().andEqualTo("activityId",activityId);
        this.deleteByExample(example);
    }

    @Override
    public List<ActivityCampus> findActivityCampusList(long activityId) {
        Example<ActivityCampus> example=ActivityCampus.createExample();
        example.createCriteria().andEqualTo("activityId",activityId);
        return this.findListByExample(example);
    }

    @Override
    public List<ActivityCampusDetailVo> findActivityCampusListByActivityId(long activityId) {

        List<ActivityCampus> activityCampusList=this.findActivityCampusList(activityId);
        if (Utils.isEmpty(activityCampusList)){
            return Collections.EMPTY_LIST;
        }
        List<ActivityCampusDetailVo> activityCampusDetailVoList=new ArrayList<>(activityCampusList.size());
        List<Long> activityCampusIds = BeanUtils.toList(activityCampusList, "activityCampusId");
        List<Long> campusIds = BeanUtils.toList(activityCampusList, "campusId");
        List<Campus> campusList=campusService.findByIds(campusIds);
        Map<Long, Campus> campusMap = BeanUtils.toMap(campusList, "campusId");

        List<ActivityRegSituation> activityRegSituationList = activityRegSituationService.findActivityRegSituationByActivityIdCampusIds(activityId, campusIds);
        List<ActivityCampusSession> activityCampusSessionList=activityCampusSessionService.findSessionByActivityCampusIds(activityCampusIds);
        Map<Long, List<ActivityCampusSession>> campusSessionMap = BeanUtils.toGroup(activityCampusSessionList, "activityCampusId");
        Map<Long,List<ActivityRegSituation>> map=BeanUtils.toGroup(activityRegSituationList,"campusId");
        for (ActivityCampus activityCampus:activityCampusList){
            ActivityCampusDetailVo activityCampusDetailVo=new ActivityCampusDetailVo();
            BeanUtils.copyProperties(activityCampusDetailVo,activityCampus);
            if (map.containsKey(activityCampus.getCampusId())){
                activityCampusDetailVo.setCanChange(false);
            }else {
                activityCampusDetailVo.setCanChange(true);
            }
            activityCampusDetailVo.setCampusName(campusMap.get(activityCampus.getCampusId()).getCampusName());
            activityCampusDetailVo.setActivityCampusSessionList(campusSessionMap.get(activityCampus.getActivityCampusId()));
            activityCampusDetailVoList.add(activityCampusDetailVo);
        }

        return activityCampusDetailVoList;
    }

    @Override
    public List<ActivityCampusDetailVo> findCanRegActivityCampusByActivityId(long activityId) {
        List<ActivityCampus> activityCampusList=this.findActivityCampusList(activityId);
        if (Utils.isEmpty(activityCampusList)){
            return Collections.EMPTY_LIST;
        }
        List<ActivityCampusDetailVo> activityCampusDetailVoList=new ArrayList<>(activityCampusList.size());
        List<Long> activityCampusIds = BeanUtils.toList(activityCampusList, "activityCampusId");
        List<Long> campusIds = BeanUtils.toList(activityCampusList, "campusId");
        List<Campus> campusList=campusService.findByIds(campusIds);
        Map<Long, Campus> campusMap = BeanUtils.toMap(campusList, "campusId");


        List<ActivityCampusSession> activityCampusSessionList=activityCampusSessionService.findSessionByActivityCampusIdsShowStatus(activityCampusIds,1);
        Map<Long, List<ActivityCampusSession>> campusSessionMap = BeanUtils.toGroup(activityCampusSessionList, "activityCampusId");

        for (ActivityCampus activityCampus:activityCampusList){
            ActivityCampusDetailVo activityCampusDetailVo=new ActivityCampusDetailVo();
            BeanUtils.copyProperties(activityCampusDetailVo,activityCampus);
            activityCampusDetailVo.setCampusName(campusMap.get(activityCampus.getCampusId()).getCampusName());
            activityCampusDetailVo.setActivityCampusSessionList(campusSessionMap.get(activityCampus.getActivityCampusId()));
            if (!Utils.isEmpty(activityCampusDetailVo.getActivityCampusSessionList())){
                activityCampusDetailVoList.add(activityCampusDetailVo);
            }
        }

        return activityCampusDetailVoList;
    }

    @Override
    public void updateActivityNameAndStartDateByActivityId(long activityId, String activityName, Date activityStartDate) {
        ActivityCampus activityCampus=new ActivityCampus();
        activityCampus.setActivityName(activityName);
        activityCampus.setActivityStartDate(activityStartDate);
        Example<ActivityCampus> example=ActivityCampus.createExample();
        example.createCriteria().andEqualTo("activityId",activityId);
        this.updateSelectiveByExample(activityCampus,example);
    }

    @Override
    public Page<ActivityCampusDetailVo> findActivityTargetPageByQueryVo(DataPermission dataPermission, ActivityCampusQueryVo activityCampusQueryVo, int pageNo, int pageSize) {
        Example<ActivityCampus> example=ActivityCampus.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,activityCampusQueryVo);
        criteria.andIn("campusId",dataPermission.getCampusIds());
        example.orderBy("createTime").desc();
        Page<ActivityCampus> activityCampusPage = this.findPageByExample(example, pageNo, pageSize);
        Page<ActivityCampusDetailVo> activityCampusDetailVoPage=Utils.newPage(activityCampusPage);
        List<ActivityCampus> activityCampusList=activityCampusPage.getRecords();
        if (activityCampusList!=null){
            List<Long> campusIds = BeanUtils.toList(activityCampusList, "campusId");
            List<Campus> campusList=campusService.findByIds(campusIds,"campusId","campusName");
            Map<Long,Campus> campusMap=BeanUtils.toMap(campusList,"campusId");

            List<Long> activityCampusIds=BeanUtils.toList(activityCampusList,"activityCampusId");
            List<ActivityCampusSession> activityCampusSessionList=activityCampusSessionService.findSessionByActivityCampusIds(activityCampusIds);
            Map<Long, List<ActivityCampusSession>> campusSessionMap = BeanUtils.toGroup(activityCampusSessionList, "activityCampusId");

            List<ActivityCampusDetailVo> activityTargetDetailVoList= Lists.transform(activityCampusList, item->{
                ActivityCampusDetailVo activityCampusDetailVo=new ActivityCampusDetailVo();
                BeanUtils.copyProperties(activityCampusDetailVo,item);
                Campus campus=campusMap.get(item.getCampusId());
                activityCampusDetailVo.setCampusName(campus.getCampusName());
                activityCampusDetailVo.setActivityCampusSessionList(campusSessionMap.get(item.getActivityCampusId()));
                return activityCampusDetailVo;
            });
            activityCampusDetailVoPage.setRecords(activityTargetDetailVoList);
        }
        return activityCampusDetailVoPage;
    }
}